<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.org/ui"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:pe="http://primefaces.org/ui/extensions" 
                template="/WEB-INF/template/template.xhtml" >

    <ui:define name="Content">

        <ui:param name="bean" value="#{sistemaMBean}" />

        <p:fieldset legend="Visualizando 'Sistemas Desenvolvidos'" rendered="#{bean.editMode eq false}">
            <h:form id="panelView" prependId="false">
                
                <p:dataTable widgetVar="systemTable" var="sistema"
                             value="#{bean.sistemas}" filterEvent="enter"
                             selection="#{bean.selectedSistema}" paginatorAlwaysVisible="false"  
                             emptyMessage="Sem Registros" paginator="true" rows="10"
                             paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" 
                             rowsPerPageTemplate="10,25,50,100"
                             currentPageReportTemplate="(Displaying {startRecord} - {endRecord} of {totalRecords}, Page: {currentPage}/{totalPages})">

                    <p:column headerText="ID" sortBy="#{sistema.id}" filterBy="#{sistema.id}" footerText="teste" filterMatchMode="contains">
                        <h:outputText value="#{sistema.id}" />
                    </p:column>

                    <p:column headerText="Nome" sortBy="#{sistema.nome.toLowerCase()}" filterBy="#{sistema.nome}" filterMatchMode="contains">
                        <h:outputText value="#{sistema.nome}" />
                    </p:column>

                    <p:column headerText="Sigla" sortBy="#{sistema.sigla.toLowerCase()}" filterBy="#{sistema.sigla}" filterMatchMode="contains" >
                        <h:outputText value="#{sistema.sigla}" />
                    </p:column>

                    <p:column headerText="Descricao" sortBy="#{sistema.descricao.toLowerCase()}" filterBy="#{sistema.descricao}" filterMatchMode="contains">
                        <h:outputText value="#{sistema.descricao}" />
                    </p:column>

                    <p:column headerText="Linguagem" sortBy="#{sistema.linguagem.toLowerCase()}" filterBy="#{sistema.linguagem}" filterMatchMode="contains" >
                        <h:outputText value="#{sistema.linguagem}" />
                    </p:column>

                    <p:column headerText="Actions" style="text-align: center" >
                        <p:commandButton icon="ui-icon-pencil" action="#{bean.edit()}" update=":pageCentral" global="false">
                            <f:setPropertyActionListener value="#{sistema}" target="#{bean.selectedSistema}" />
                        </p:commandButton>

                        <p:commandButton icon="ui-icon-trash" onsuccess="deleteConfirm.show()" global="false">
                            <f:setPropertyActionListener value="#{sistema}" target="#{bean.selectedSistema}" />
                        </p:commandButton>
                    </p:column>

                    <f:facet name="footer">
                        <p:commandButton value="Novo" action="#{bean.editAndReset()}" icon="ui-icon-document" global="false" 
                                         update=":pageCentral" process="@this" />

                        <p:commandButton value="Voltar" action="/systems?faces-redirect=true"
                                         icon="ui-icon-check" global="false" process="@this" />
                    </f:facet>
                </p:dataTable>
                
            </h:form>
        </p:fieldset>

        <p:fieldset legend="Editando #{bean.selectedSistema.nome}" rendered="#{bean.editMode eq true}">
            <h:form id="panelEdit" prependId="false">
                
                <p:focus context="details" />

                <h:panelGrid id="details" columns="2" cellspacing="10" > 

                    <p:outputLabel value="Nome:" for="name" style="font-weight: bold"/>
                    <p:inputText label="Name" id="name" value="#{bean.selectedSistema.nome}" required="true" 
                                 title="#{component.valid ? 'Enter Name' : 'Name is empty or contains invalid caracters.'}" >
                        <pe:keyFilter regEx="/^[a-zA-Z\s-]+/" />  
                        <f:validateRegex pattern="^([a-zA-Z\s-]+)$" />
                    </p:inputText>

                    <p:outputLabel value="Sigla:" for="sigla" style="font-weight: bold"/>
                    <p:inputText label="Sigla" id="sigla" value="#{bean.selectedSistema.sigla}" required="true" 
                                 title="#{component.valid ? 'Enter Sigla' : 'Sigla is empty or contains invalid caracters.'}"/>

                    <p:outputLabel value="Descricao:" for="descricao" style="font-weight: bold"/>
                    <p:inputText label="Descrição" id="descricao" value="#{bean.selectedSistema.descricao}" required="true" 
                                 title="#{component.valid ? 'Enter Descrição' : 'Descrição is empty or contains invalid caracters.'}"/>

                    <p:outputLabel value="Linguagem:" for="linguagem" style="font-weight: bold"/>
                    <p:inputText label="Linguagem" id="linguagem" value="#{bean.selectedSistema.linguagem}" required="true" 
                                 title="#{component.valid ? 'Enter Linguagem' : 'Linguagem is empty or contains invalid caracters.'}"/>

                    <f:facet name="footer">
                        <p:commandButton value="Cadastrar" action="#{bean.salvar}" 
                                         update=":pageCentral" icon="ui-icon-check" />

                        <p:commandButton value="Voltar" icon="ui-icon-cancel" action="#{bean.cancelEdit()}"   
                                         update=":pageCentral" global="false" process="@this" />
                    </f:facet>
                </h:panelGrid> 
                
            </h:form>
        </p:fieldset>

        <p:confirmDialog id="deleteConfirmId" message="Are you sure you want to remove this?" 
                         header="Confirmation" widgetVar="deleteConfirm" appendToBody="true" severity="alert">
            <h:form>
                <p:commandButton value="No" onsuccess="deleteConfirm.hide()" icon="ui-icon-cancel" 
                                 style="float: right;" global="false"/>

                <p:commandButton value="Yes" onsuccess="deleteConfirm.hide()" update=":panelView" 
                                 action="#{bean.delete}" icon="ui-icon-check" style="float: right;" />
            </h:form>
        </p:confirmDialog>

    </ui:define>
</ui:composition>